// hdu3371
// 题意：水题
//
// 题解：水题。好恶心。。直接交kruskal过不了，改了一发prim也过不了，尼妹。
//       加了巨型读入优化，kruskal秒过。无聊。
//
// 统计：234ms
//
// run: $exec < input
#include <cstdio>
#include <algorithm>

struct edge { int from, to, cost; };

bool operator<(edge const & a, edge const & b) { return a.cost < b.cost; }

int const maxn = 505;
int const maxm = 25010;
int parent[maxn];
int rank[maxn];
int n, m, k;

edge edges[maxm];

int get_parent(int u)
{
	return (parent[u] == u) ? u : parent[u] = get_parent(parent[u]);
}

void set_union(int u, int v)
{
	int tu = get_parent(u);
	int tv = get_parent(v);
	if (tu != tv) {
		if (rank[tu] < rank[tv]) parent[tu] = tv;
		else {
			parent[tv] = tu;
			if (rank[tu] == rank[tv]) rank[tu]++;
		}
	}
}


const int BZ = 30 << 20;
char Buf[BZ + 1], *buf = Buf;

template <class T>
inline void scan(T &a) // method2: for huge input
{
	bool flag = false;
	for (a = 0; *buf < '0' || *buf > '9'; ++buf)
		if (*buf == '-') flag = true;
	for (; *buf >= '0' && *buf <= '9'; buf++)
		a = a * 10 + (*buf - '0');
	if (flag) a = -a;
}

int main()
{
	std::fread(Buf, 1, BZ, stdin); // way2
	int T;
//	std::scanf("%d", &T);
	scan(T);
	while (T--) {
//		std::scanf("%d %d %d", &n, &m, &k);
		scan(n); scan(m); scan(k);
		for (int i = 0; i < m; i++) {
			//std::scanf("%d %d %d", &edges[i].from, &edges[i].to, &edges[i].cost);
			scan(edges[i].from); scan(edges[i].to); scan(edges[i].cost);
		}

		for (int i = 1; i <= n; i++) { parent[i] = i; rank[i] = 1; }

		for (int i = 0, t, prev, now; i < k; i++) {
			scan(t); scan(prev);
//			std::scanf("%d", &t); std::scanf("%d", &prev);
			for (int j = 1; j < t; j++) {
//				std::scanf("%d", &now);
				scan(now);
				set_union(now, prev);
				prev = now;
			}
		}

		for (int i = 0; i < m; i++) {
			while (i < m && get_parent(edges[i].from) == get_parent(edges[i].to)) {
				std::swap(edges[i], edges[--m]);
			}
		}

		std::sort(edges, edges + m);

		/*
		for (int i = 1; i <= n; i++) std::printf("%d ", parent[i]);
		std::printf("\n");
		*/

		int ans = 0;
		for (int i = 0; i < m; i++) {
			int u = edges[i].from, v = edges[i].to, c = edges[i].cost;
			if (get_parent(u) == get_parent(v)) continue;
			ans += c;
			set_union(u, v);
		}

		for (int i = 1; i <= n; i++) parent[i] = get_parent(parent[i]);
		/*
		for (int i = 1; i <= n; i++) std::printf("%d ", parent[i]);
		std::printf("\n");
		*/

		for (int i = 2; i <= n; i++)
			if (parent[i] != parent[i - 1]) { ans = -1; break; }
		std::printf("%d\n", ans);
	}
}

